<template>
  <div>
    <div class="main">
      <div class="py-container">
        <div class="bread">
          <ul class="fl sui-breadcrumb">
            <li>
              <h4 @click="goback">首页<i class="crumbs-arrow">></i></h4>
            </li>
          </ul>
        </div>

        <SearchSelector @shoesInfoByBrand='shoesInfoByBrand' @shoesInfoByColor='shoesInfoByColor' @shoesInfoBySize='shoesInfoBySize' @shoesInfoByType='shoesInfoByType'/>
        <div class="details clearfix">
          <div class="goods-list">
            <ul class="yui3-g">
              <li class="yui3-u-1-5" v-for="(shoes,index) in shoesList.data" :key="index">
                <div class="list-wrap" >
                  <div class="p-img" @click="godetail(shoes.sid)">
                    
                    <a target="_blank" v-if="shoes.shoePhotos != ''"
                      ><img :src="`images${shoes.simage}`"
                    /></a>
                    <a target="_blank" v-if="shoes.shoePhotos == ''"
                      ><img src="images/shoes.jpg"
                    /></a>
                  </div>
                  <div class="good-name" @click="godetail(shoes.sid)">
                    <a target="_blank" title="">{{shoes.sname}}</a>
                  </div>
                  <div class="attr">
                    <i class="good-attr"
                      ><span>{{shoes.bname}}</span><span>{{shoes.tname}}</span></i
                    >
                  </div>
                  <div class="price">
                    <span class="old-price">
                    
                      <del>{{shoes.sprices}}</del>
                      <i>0.67</i>
                    </span>

                    <span class="now-price">
                      <strong>
                        <em>¥</em>
                        <i>{{shoes.sdiscount}}</i>
                      </strong>
                    </span>
                  </div>
                </div>
              </li>
            </ul>
          </div>
          <Pagination
            :pageNo="searchParams.pageNum"
            :pageSize="searchParams.pageSize"
            :total=count
            :continues="5"
            @getPageNo="getPageNo"
          />

        </div>
      </div>
    </div>
    <MyFoot />
  </div>
</template>

<script>
import SearchSelector from "./SearchSelector/SearchSelector";
import Pagination from "./Pagination";
import {createLogger, mapState} from "vuex";

export default {
  name: "Search",
  data() {
    return {
      searchBrand: "",
      searchParams: {
        "pageNum": 1,
        "pageSize": 10,
        "sbids": [],
        "scids": [],
        "sgenders": [],
        "sizeids": [],
        "stids": [],
      },
      query: {
        "bid": '',
        "cid": '',
        "tid": '',
        "sid": ''
      }
    };
  },
  components: {
    SearchSelector,
    Pagination,
  },
  methods: {
    getData() {
      this.$store.dispatch("getShoesList", {
        pageNum: this.searchParams.pageNum,
        pageSize: this.searchParams.pageSize,
        sbids: this.searchParams.sbids,
        scids: this.searchParams.scids,
        sgenders: this.searchParams.sgenders,
        sizeids: this.searchParams.sizeids,
        stids: this.searchParams.stids,
      });
    },
    getShoesByBrandId() {
      this.$store.dispatch('getShoesListByBrands',this.bid);
    },
    shoesInfoByBrand(bid) {
      this.query.bid = bid;
      this.searchParams.sbids.splice(0,1,bid);
    },
    shoesInfoByColor(cid) {
      this.query.cid = cid;
      this.searchParams.scids.splice(0,1,cid);
    },
    shoesInfoByType(tid) {
      this.query.tid = tid;
      this.searchParams.stids.splice(0,1,tid);
    },
    shoesInfoBySize(sid) {
      this.query.sid = sid;
      this.searchParams.sizeids.splice(0,1,sid);
    },
    getPageNo(pageNum){
      this.searchParams.pageNum = pageNum;
      this.getData();
    },
    godetail(sid) {
      this.$router.push({name: "detail", query: {sid: sid}});
    },
    goback() {
      this.$router.push({name: 'Home'});
    }
    
  },
  beforeMount() {
    if(this.$route.query.bid != ''){
      this.searchParams.sbids.splice(0,1,this.$route.query.bid);
    };
    if(this.$route.query.bid == '' && this.$route.query.cid == '' && this.$route.query.sid == '' && this.$route.query.tid == ''){
      this.$router.push({name: "search"})
    }
  },
  mounted() {
    this.getData();
    this.searchParams.sbids = [];
    this.searchParams.stids = [];
    this.searchParams.sizeids = [];
    this.searchParams.scids = [];
  },
   watch: {
    // 监听路由的信息是否发生变化，如果发生变化，就再次发生请求
    searchParams: {
      handler(newVal,oldVal) {
        this.$router.push({name: "search", query: this.query});       
        this.getData();
      },
      deep: true
    }
  },
  computed: {
    ...mapState({
      shoesList: (state) => state.search.shoesList,
      count: (state) => state.search.shoesList.count,
      page: (state) => state.search.shoesList.page,
    }),
  }
};
</script>

<style lang="less" scoped>
div {
  background-color: #f5f8fa;
  .main {
    .py-container {
      width: 1200px;
      margin: 0 auto;

      .bread {
        margin-bottom: 5px;
        overflow: hidden;
        height: 40px;

        .sui-breadcrumb {
          padding: 10px 15px;
          margin: 0;
          font-weight: 400;
          border-radius: 3px;
          float: left;
          h4 {
            font-size: 15px;
          }
          i {
            margin-left: 10px;
            cursor: pointer;
            font: 400 14px tahoma;
            display: inline-block;
            height: 100%;
            font-weight: 700;
            color: #d3d5db;
          }

          li {
            display: inline-block;
            line-height: 18px;

            a {
              color: #666;
              text-decoration: none;

              &:hover {
                color: #4cb9fc;
              }
            }
          }
        }

        .sui-tag {
          list-style: none;
          font-size: 14px;
          margin-bottom: 18px;
          float: left;
          height: 40px;
          line-height: 40px;
          li {
            display: inline-block;
            padding-top: -2px;
            font-size: 14px;
            i {
              cursor: pointer;
              font: 400 14px tahoma;
              display: inline-block;
              height: 100%;
              font-weight: 700;
              color: #d3d5db;
              line-height: 14px;
              padding: 0 5px;
            }

            &:hover {
              color: #28a3ef;
            }
          }
        }
      }
    }

    .details {
      margin-bottom: 10px;
      h2 {
        font-weight: 700;
        font-size: 30px;
      }
      .sui-navbar {
        overflow: visible;
        margin-bottom: 0;

        .filter {
          min-height: 40px;
          padding-right: 20px;
          background: #fbfbfb;
          border: 1px solid #e2e2e2;
          padding-left: 0;
          border-radius: 0;
          box-shadow: 0 1px 4px rgba(0, 0, 0, 0.065);

          .sui-nav {
            position: relative;
            left: 0;
            display: block;
            float: left;
            margin: 0 10px 0 0;

            li {
              float: left;
              line-height: 18px;

              a {
                display: block;
                cursor: pointer;
                padding: 11px 15px;
                color: #777;
                text-decoration: none;
              }

              &.active {
                a {
                  background: #0ae99e;
                  color: #fff;
                }
              }
            }
          }
        }
      }

      .goods-list {
        margin: 20px 0;

        ul {
          display: flex;
          flex-wrap: wrap;

          li {
            height: 100%;
            width: 20%;
            margin-top: 10px;
            line-height: 28px;

            .list-wrap {
              .p-img {
                padding-left: 15px;
                width: 215px;
                height: 230px;

                a {
                  color: #666;

                  img {
                    max-width: 100%;
                    height: auto;
                    vertical-align: middle;
                  }
                }
              }

              .price {
                margin: 0 auto;
                .old-price {
                  display: inline-block;
                  color: #c5c2c2;
                  font-size: 14px;
                  width: 100%;
                  del {
                    padding-right: 8px;
                  }
                  i {
                    font-size: 16px;
                    color: #ec8712;
                  }
                }
                font-size: 18px;
                color: #c81623;

                strong {
                  font-weight: 700;
                }
              }

              .good-name {
                width: 100%;
                overflow: hidden;
                margin: 0 auto;
                cursor: pointer;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;

                a {
                  color: rgb(94, 84, 84);
                  font-size: 20px;
                  text-decoration: none;
                }
              }

              .attr {
                margin: 0 auto;
                height: 22px;
                font-size: 13px;
                color: #a7a7a7;

                span {
                  color: #a7a7a7;
                  padding: 0 2px;
                }
              }

              .operate {
                padding: 12px 15px;

                .sui-btn {
                  display: inline-block;
                  padding: 2px 14px;
                  box-sizing: border-box;
                  margin-bottom: 0;
                  font-size: 12px;
                  line-height: 18px;
                  text-align: center;
                  vertical-align: middle;
                  cursor: pointer;
                  border-radius: 0;
                  background-color: transparent;
                  margin-right: 15px;
                }

                .btn-bordered {
                  min-width: 85px;
                  background-color: transparent;

                  &:hover {
                    border: 1px solid #666;
                    color: #fff !important;
                    text-decoration: none;
                  }
                }

                .btn-danger,
                .btn-bordered {
                  border: 1px solid #e1251b;
                  color: #e1251b;

                  &:hover {
                    border: 1px solid #e1251b;
                    background-color: #e1251b;
                    color: white !important;
                    text-decoration: none;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>